<?php

namespace app\rental\model;

use app\admin\model\RouteModel;
use think\Model;
use tree\Tree;
use think\Db;

class RentalCategoryModel extends Model
{

    protected $type = [
        'more' => 'array',
    ];
    /**
     * 关联汽车
     */
    public function cars()
    {
        return $this->hasMany('RentalCarModel','category_id');
    }

    public function getAliasAttr($value,$data){
        $id = $data['id'];
        $routeModel = new RouteModel();
        return $routeModel->getUrl('rental/List/index', ['id' => $id]);
    }

    public function getCategorys(){
        return $this->where(['status'=>1,'delete_time'=>0])->select();
    }
    public function getIndexCategorys(){
        $category = $this->where(['status'=>1,'delete_time'=>0])->select();
        foreach($category as $key=>$val){
         $category[$key]->car =  $val->cars()->order('price  asc')->limit(1)->field('car_model,title,price')->find();
        }
        return $category;
    }
    /*获取租赁分类 */
    public function getRentalCategory(){
        $theme_file_more =  Db::name('theme_file')->where(['theme' => 'echehui', 'action' => 'rental/Index/index'])->find();
        $more = json_decode($theme_file_more['more'],true);
        return  $more['widgets']['rental_category']['vars']['rental_category_id']['value'];
    }

}
